package com.zh.amqp.rabbit.mqmsg;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * @author ZH
 * @date 14:10 2020/8/19
 */
@Slf4j
@AllArgsConstructor
@Component
public class MsgProducer {
    public static final String DEMO_EXCHANGE = "demoExchange";
    public static final String ROUTING_KEY = "demo.index";
    public static final String DEMO_QUEUE = "demoQueue";

    private final RabbitTemplate rabbitTemp;

    /**
     * 发送 MQ消息
     * @param msg
     */
    public void sendMsg(String msg) {
        String randomId = String.valueOf(System.nanoTime());

        MqMsg mqMsg = new MqMsg(randomId, msg);
        rabbitTemp.convertAndSend(DEMO_EXCHANGE, ROUTING_KEY, mqMsg, (message) -> message, new CorrelationData( mqMsg.getId()));
//        rabbitTemp.convertAndSend(DEMO_EXCHANGE, ROUTING_KEY+"EE", mqMsg, (message) -> message, new CorrelationData( ));
    }

}
